package com.mentaleye.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.StudentInterviewRecordBean;
import com.mentaleye.biz.StudentInterviewRecordBiz;
import com.mentaleye.dal.model.StudentInterviewRecord;
import com.mentaleye.dal.model.Students;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.service.StudentInterviewRecordService;
import com.mentaleye.service.StudentsService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.DateFormatter;
import com.mentaleye.util.ObjectUtil;

@Service
public class StudentInterviewRecordBizImpl implements StudentInterviewRecordBiz {

	private static Logger logger = Logger.getLogger(StudentInterviewRecordBiz.class);

	@Autowired
	private StudentInterviewRecordService recordService;

	@Autowired
	private StudentsService studentService;

	public CommonResult insertRecord(StudentInterviewRecordBean recordBean) {
		CommonResult result = new CommonResult();
		try {
			recordBean.setInterviewTime(DateFormatter.parse(recordBean.getInterviewTimeString(), "yyyy-MM-dd"));
			recordBean.setEntryTime(DateFormatter.parse(recordBean.getEntryTimeString(), "yyyy-MM-dd"));
			StudentInterviewRecord record = (StudentInterviewRecord) ObjectUtil.transfer(recordBean,
					StudentInterviewRecord.class);
			if (this.recordService.insertRecord(record)) {
				if (record.getIsEntry()) {
					Students student = new Students();
					student.setId(record.getStudentId());
					student.setState(DictionaryEnums.EMPLOYMENT.getCode());
					this.studentService.updateStateById(student);
				}
				
				result.setIsSuccess(true);
				result.setResultMsg("插入成功");
			} else {
				result.setResultMsg("插入失败");
			}
		} catch (Exception e) {
			logger.error("插入面试记录异常", e);
			result.setResultMsg("插入面试记录异常");
		}
		return result;
	}

	public CommonResult updateRecord(StudentInterviewRecordBean recordBean) {
		CommonResult result = new CommonResult();
		try {
			recordBean.setInterviewTime(DateFormatter.parse(recordBean.getInterviewTimeString(), "yyyy-MM-dd"));
			recordBean.setEntryTime(DateFormatter.parse(recordBean.getEntryTimeString(), "yyyy-MM-dd"));
			StudentInterviewRecord record = (StudentInterviewRecord) ObjectUtil.transfer(recordBean,
					StudentInterviewRecord.class);
			if (this.recordService.updateRecord(record)) {
				if (record.getIsEntry()) {
					Students student = new Students();
					student.setId(record.getStudentId());
					student.setState(DictionaryEnums.EMPLOYMENT.getCode());
					this.studentService.updateStateById(student);
				}

				result.setIsSuccess(true);
				result.setResultMsg("修改成功");
			} else {
				result.setResultMsg("修改失败");
			}
		} catch (Exception e) {
			logger.error("修改面试记录异常", e);
			result.setResultMsg("修改面试记录异常");
		}
		return result;
	}

	public StudentInterviewRecordBean getBeanByKey(Long recorId) {
		StudentInterviewRecordBean recordBean = null;
		try {
			recordBean = (StudentInterviewRecordBean) ObjectUtil.transfer(this.recordService.getRecordByKey(recorId),
					StudentInterviewRecordBean.class);
		} catch (Exception e) {
			logger.error("查询面试记录异常", e);
		}
		return recordBean;
	}

	public Map<String, Object> getRecordById(long recordId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("recordId", recordId);
		try {
			resultMap = this.recordService.getRecordByMap(paramMap);
		} catch (Exception e) {
			logger.error("查询面试记录异常", e);
		}
		return resultMap;
	}

	@SuppressWarnings("unchecked")
	public List<StudentInterviewRecordBean> getBeanList() {
		List<StudentInterviewRecordBean> beanList = null;
		try {
			beanList = ObjectUtil.transferList(this.recordService.getRecordList(), StudentInterviewRecordBean.class);
		} catch (Exception e) {
			logger.error("查询面试记录异常", e);
		}
		return beanList;
	}

	public void getListByPage(CommonPage page) {
		try {
			page.setPageData(this.recordService.getListByMap(page.pageToMap()));
		} catch (Exception e) {
			logger.error("查询面试记录异常", e);
		}
	}

	public CommonResult deleteByKeyList(List<Long> idList) {
		CommonResult result = new CommonResult();
		Map<String,Object> map=new HashMap<String, Object>();
		List<Integer> studentKeyList=new ArrayList<Integer>();
		try {
			List<StudentInterviewRecord> recordList = this.recordService.getListByKeys(idList);
			if (this.recordService.deleteByKeyList(idList)) {
				for (StudentInterviewRecord record : recordList) {
					if (record.getIsEntry()) {
						studentKeyList.add(record.getStudentId());
					}
				}
				
				map.put("idList", studentKeyList);
				map.put("state", DictionaryEnums.NO_EMPLOYMENT.getCode());
				this.studentService.updateStateByMap(map);
				
				result.setIsSuccess(true);
				result.setResultMsg("删除成功");
			} else {
				result.setResultMsg("删除失败");
			}
		} catch (Exception e) {
			logger.error("删除面试记录异常", e);
			result.setResultMsg("删除面试记录异常");
		}
		return result;
	}

}
